
package ar.edu.unq.svnpermission.ui.pages;


import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.CompoundPropertyModel;

import ar.edu.unq.svnpermission.domain.Folder;
import ar.edu.unq.svnpermission.panels.FolderDetailPanel;
import ar.edu.unq.svnpermission.panels.TreePanel;
import ar.edu.unq.svnpermission.ui.model.Folders;

/**
 * @author nicolas
 *
 */
public class PermissionManagerPage extends InternalPage<Folders> {
	private static final long serialVersionUID = 1L;
	

	
	public PermissionManagerPage()
	{
		super();
	}
	
	@Override
	protected void addFields() {
		FolderDetailPanel folderDetailPanel = new FolderDetailPanel("folderDetail",this.getForm());
		folderDetailPanel.setOutputMarkupPlaceholderTag(true);
		new TreePanel("tree", this.getForm());		
	}

	@Override
	protected void addButtons() {
	}

	@Override
	protected CompoundPropertyModel<Folders> createModel() {
		return new CompoundPropertyModel<Folders>(new Folders());
	}

	@Override
	protected String getFormName() {
		return "treeForm";
	}
	
	protected void renderTree()
	{
		final Folders folderModel = (Folders)this.getForm().getDefaultModelObject();
		RepeatingView rv = new RepeatingView("rootFolder");
		this.getForm().add(rv);

		WebMarkupContainer parent = new WebMarkupContainer(rv.newChildId());
		rv.add(parent);

		AjaxFallbackLink<Folder> rootFolderLink = new AjaxFallbackLink<Folder>("rootFolderLink") {		
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				//Seteo la carpeta seleccionada como la SelectedFolder
				//TODO: modificar despues que se arme la iteracion, para que tome la carpeta del link
				folderModel.setSelectedFolder(folderModel.getRootFolder());
				target.add(this.getPage().get("treeForm").get("folderDetail"));
				this.getPage().get("treeForm").get("folderDetail").setVisible(true);
				
			}
		};

		parent.add(rootFolderLink);
		
		rootFolderLink.add(new Label("rootFolderCaption",folderModel.getRootFolder().getName()));
		
		
	}
	

}
